<?php
require_once 'common.inc.php';
$hotfile="../push/filter.php";//字典文件
$dhtfile="../push/otherconfig.php";//字典文件
$trafile="../push/traconfig.php";//字典文件
$webfile="../push/webconfig.php";//字典文件

$file='../disposeturnip.php';
$hostlist=file_get_contents($file);
$hostmp=explode(';',$hostlist);
$num=count($hostmp)-1;
if(empty($hostmp[$num]))unset($hostmp[$num]);

foreach($hostmp as $value){
	$result=explode(':',$value);
	$host[]=$result[0];
	$port[]=$result[1];
}

function sendin($msg, $host, $port) {
	$fp = fsockopen($host, $port, $errno, $errstr, 30);
	if (!$fp) {
		echo 'sorry!';
	} else {
		fwrite($fp, $msg); 
		fclose($fp);
	} 
} 

function batsendin($msg, $host, $port, $unique) {
	if(!$unique){
	foreach($host as $list) {
		foreach($port as $value) {
			sendin($msg, $list, $value);
			//echo $list."<br />";
			//echo $value."<br />";
		} 
	} 
	}else{
		foreach($host as $key=>$list){
			sendin($msg,$list,$port[$key]);
			//echo $list."<br />";
			//echo $port[$key]."<br />";
		}
	}
}

error_reporting(E_ALL);
function sendin($msg,$host,$port)
{
        $fp=fsockopen($host,$port,$errno,$errstr,30);
        if(!$fp)
        {
                echo 'sorry!';
        }
        else
        {
                fwrite($fp,$msg);
//      echo 'fuck2';
                fclose($fp);
        }
}

if ('list'==$action && $name=='isdht' || $name=='ishot' || $name=='isweb' || $name=='istra')
{
   
        if($name=='ishot')
        {
                $fp=fopen($hotfile,'wb+');
                if($value=='1')
                {
                        if($fp)
                        {
                                fwrite($fp,'1');
                                fclose($fp);
                                adminmsg('显示热门列表设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('显示热门列表设置失败','setturn.inc.php?action=list');
                }else if($value=='-1')
                {
                        if($fp)
                        {
                                fwrite($fp,'２');
                                fclose($fp);
                                adminmsg('关闭热门列表设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('关闭热门列表设置失败','setturn.inc.php?action=list');
                }
        }
        elseif($name=='isdht')
        {


                $dirhander = opendir('../push/torrent');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/torrent/$file");
                        }
                }

                //读取文件夹所有文件，循环删除文件

                $dirhander = opendir('../push/attach');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/attach/$file");
                        }
                }

                //读取文件夹所有文件，循环删除文件

                $dirhander = opendir('../push/dhttorrent');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/dhttorrent/$file");
                        }
                }

                $sql="truncate table ps_downinfo";
                $dp->query($sql);
                $sql="truncate table ps_dht";
                $dp->query($sql);

                $fp=fopen($dhtfile,'wb+');
                if($value=='1')
                {
				batsendin('1',$host,$port,true);

                        if($fp)
                        {
                                fwrite($fp,'1');
                                fclose($fp);
                                adminmsg('开启DHT设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('开启DHT设置失败','setturn.inc.php?action=list');
                }else 
                if($value=='-1')
                {
				batsendin('2',$host,$port,true);

 
                        if($fp)
                        {
                                fwrite($fp,'2');
                                fclose($fp);
                                adminmsg('关闭DHT设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('关闭DHT设置失败','setturn.inc.php?action=list');
                }

        }
        elseif($name=='istra')
        {


                $dirhander = opendir('../push/torrent');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/torrent/$file");
                        }
                }

                //读取文件夹所有文件，循环删除文件

                $dirhander = opendir('../push/attach');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/attach/$file");
                        }
                }

                //读取文件夹所有文件，循环删除文件

                $dirhander = opendir('../push/dhttorrent');
                while ($file=readdir($dirhander))
                {
                        if($file!='.'&&$file!='..')
                        {
                                unlink("../push/dhttorrent/$file");
                        }
                }

                $sql="truncate table ps_downinfo";
                $dp->query($sql);
                $sql="truncate table ps_dht";
                $dp->query($sql);

                $fp=fopen($trafile,'wb+');
                if($value=='1')
                {
					batsendin('1',$host,$port,true);

                        if($fp)
                        {
                                //echo "1";
                                fwrite($fp,'1');
                                fclose($fp);
                                //die();
                                adminmsg('开启Tracker设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('开启Tracker设置失败','setturn.inc.php?action=list');
                }else 
                if($value=='-1')
                {
					batsendin('2',$host,$port,true);

                        if($fp)
                        {
                                //echo "2";
                                fwrite($fp,'2');
                                fclose($fp);
                                //die();
                                adminmsg('关闭Tracker设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('关闭Tracker设置失败','setturn.inc.php?action=list');
                }

        }elseif($name=='isweb')
        {
                $fp=fopen($webfile,'wb+');
                if($value=='1')
                {
					batsendin('3',$host,$port,true);

                        if($fp)
                        {
                                fwrite($fp,'3');
                                fclose($fp);
                                adminmsg('开启网页透明设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('开启网页透明设置失败','setturn.inc.php?action=list');
                }else if($value=='-1')
                {
					batsendin('4',$host,$port,true);

                        if($fp)
                        {
                                fwrite($fp,'4');
                                fclose($fp);
                                adminmsg('关闭网页透明设置成功','setturn.inc.php?action=list');
                        }
                        else
                                adminmsg('关闭网页透明设置失败','setturn.inc.php?action=list');
                }
        }
}else
adminmsg('参数设置错误！','setturn.inc.php?action=list');
?>